package com.google.android.exoplayer2.offline;

import android.os.Handler;
import com.huawei.appgallery.festivalanimation.particle.modifiers.SizeModifier;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;
import o.ahn;
import o.ahp;
import o.ahp.c;
import o.ahq;
import o.ahr;
import o.ahs;
import o.aht;
import o.ahu;
import o.amm;
import o.amz;

public final class DownloadManager {
    private final Handler ʻ;
    private final CopyOnWriteArraySet<b> ʼ;
    private final ArrayList<Task> ʽ;
    private final aht ˊ;
    private final int ˋ;
    private boolean ˋॱ;
    private final c[] ˎ;
    private final ahn ˏ;
    private boolean ˏॱ;
    private int ͺ;
    private final int ॱ;
    private boolean ॱˊ;
    private final ArrayList<Task> ॱॱ;
    private final Handler ᐝ;

    public static final class Task implements Runnable {
        private Throwable ʽ;
        private volatile int ˊ;
        private final ahp ˋ;
        private final int ˎ;
        private final int ˏ;
        private final DownloadManager ॱ;
        private Thread ॱॱ;
        private volatile ahu ᐝ;

        @Documented
        @Retention(RetentionPolicy.SOURCE)
        public @interface InternalState {
        }

        private Task(int i, DownloadManager downloadManager, ahp o_ahp, int i2) {
            this.ˏ = i;
            this.ॱ = downloadManager;
            this.ˋ = o_ahp;
            this.ˊ = 0;
            this.ˎ = i2;
        }

        public TaskState ˏ() {
            return new TaskState(this.ˏ, this.ˋ, ʼ(), ॱ(), ˎ(), this.ʽ);
        }

        public boolean ˋ() {
            return this.ˊ == 4 || this.ˊ == 2 || this.ˊ == 3;
        }

        public boolean ˊ() {
            return this.ˊ == 5 || this.ˊ == 1 || this.ˊ == 7 || this.ˊ == 6;
        }

        public float ॱ() {
            return this.ᐝ != null ? this.ᐝ.ॱ() : SizeModifier.STABLE_STATE_SCALE;
        }

        public long ˎ() {
            return this.ᐝ != null ? this.ᐝ.ˋ() : 0;
        }

        public String toString() {
            return super.toString();
        }

        private int ʼ() {
            switch (this.ˊ) {
                case 5:
                    return 0;
                case 6:
                case 7:
                    return 1;
                default:
                    return this.ˊ;
            }
        }

        private void ʻ() {
            if (ˋ(0, 1)) {
                this.ॱॱ = new Thread(this);
                this.ॱॱ.start();
            }
        }

        private boolean ॱॱ() {
            return this.ˊ == 0;
        }

        private void ᐝ() {
            if (ˋ(0, 5)) {
                this.ॱ.ᐝ.post(new ahr(this));
            } else if (ˋ(1, 6)) {
                ˏॱ();
            }
        }

        private /* synthetic */ void ˊॱ() {
            ˋ(5, 3);
        }

        private void ʽ() {
            if (ˋ(1, 7)) {
                DownloadManager.ˊ("Stopping", this);
                ˏॱ();
            }
        }

        private boolean ˋ(int i, int i2) {
            return ˎ(i, i2, null);
        }

        private boolean ˎ(int i, int i2, Throwable th) {
            boolean z = false;
            if (this.ˊ != i) {
                return false;
            }
            this.ˊ = i2;
            this.ʽ = th;
            if (this.ˊ != ʼ()) {
                z = true;
            }
            if (!z) {
                this.ॱ.ॱ(this);
            }
            return true;
        }

        private void ˏॱ() {
            if (this.ᐝ != null) {
                this.ᐝ.ˊ();
            }
            this.ॱॱ.interrupt();
        }

        public void run() {
            int i;
            long ˋ;
            int i2;
            DownloadManager.ˊ("Task is started", this);
            Throwable th = null;
            this.ᐝ = this.ˋ.ˏ(this.ॱ.ˊ);
            if (this.ˋ.ˏ) {
                this.ᐝ.ˏ();
            } else {
                r4 = -1;
                i = 0;
                while (!Thread.interrupted()) {
                    try {
                        this.ᐝ.ˎ();
                    } catch (IOException e) {
                        ˋ = this.ᐝ.ˋ();
                        long j;
                        if (ˋ != j) {
                            DownloadManager.ˊ("Reset error count. downloadedBytes = " + ˋ, this);
                            i2 = 0;
                        } else {
                            ˋ = j;
                            i2 = i;
                        }
                        if (this.ˊ == 1) {
                            i = i2 + 1;
                            if (i <= this.ˎ) {
                                DownloadManager.ˊ("Download error. Retry " + i, this);
                                Thread.sleep((long) ˏ(i));
                                j = ˋ;
                            }
                        }
                        throw e;
                    } catch (Throwable th2) {
                        th = th2;
                    }
                }
            }
            this.ॱ.ᐝ.post(new ahs(this, th));
        }

        private /* synthetic */ void ˎ(Throwable th) {
            if (!ˎ(1, th != null ? 4 : 2, th) && !ˋ(6, 3) && !ˋ(7, 0)) {
                throw new IllegalStateException();
            }
        }

        private int ˏ(int i) {
            return Math.min((i - 1) * 1000, 5000);
        }
    }

    public static final class TaskState {
        public final Throwable ʽ;
        public final int ˊ;
        public final int ˋ;
        public final float ˎ;
        public final long ˏ;
        public final ahp ॱ;

        @Documented
        @Retention(RetentionPolicy.SOURCE)
        public @interface State {
        }

        private TaskState(int i, ahp o_ahp, int i2, float f, long j, Throwable th) {
            this.ˋ = i;
            this.ॱ = o_ahp;
            this.ˊ = i2;
            this.ˎ = f;
            this.ˏ = j;
            this.ʽ = th;
        }
    }

    public interface b {
        void ˊ(DownloadManager downloadManager);

        void ˋ(DownloadManager downloadManager, TaskState taskState);
    }

    public void ˊ(b bVar) {
        this.ʼ.add(bVar);
    }

    public void ˋ(b bVar) {
        this.ʼ.remove(bVar);
    }

    public void ˋ() {
        amm.ˏ(!this.ˋॱ);
        if (this.ॱˊ) {
            this.ॱˊ = false;
            ᐝ();
            ॱ("Downloads are started");
        }
    }

    public void ˊ() {
        boolean z;
        int i = 0;
        if (this.ˋॱ) {
            z = false;
        } else {
            z = true;
        }
        amm.ˏ(z);
        if (!this.ॱˊ) {
            this.ॱˊ = true;
            while (i < this.ʽ.size()) {
                ((Task) this.ʽ.get(i)).ʽ();
                i++;
            }
            ॱ("Downloads are stopping");
        }
    }

    public int ˋ(byte[] bArr) throws IOException {
        amm.ˏ(!this.ˋॱ);
        return ˋ(ahp.ˏ(this.ˎ, new ByteArrayInputStream(bArr)));
    }

    public int ˋ(ahp o_ahp) {
        amm.ˏ(!this.ˋॱ);
        Task ˏ = ˏ(o_ahp);
        if (this.ˏॱ) {
            ॱॱ();
            ᐝ();
            if (ˏ.ˊ == 0) {
                ˊ(ˏ);
            }
        }
        return ˏ.ˏ;
    }

    public int ˏ() {
        int i = 0;
        for (int i2 = 0; i2 < this.ॱॱ.size(); i2++) {
            if (!((Task) this.ॱॱ.get(i2)).ˋ.ˏ) {
                i++;
            }
        }
        return i;
    }

    public TaskState[] ˎ() {
        boolean z;
        int i = 0;
        if (this.ˋॱ) {
            z = false;
        } else {
            z = true;
        }
        amm.ˏ(z);
        TaskState[] taskStateArr = new TaskState[this.ॱॱ.size()];
        while (i < taskStateArr.length) {
            taskStateArr[i] = ((Task) this.ॱॱ.get(i)).ˏ();
            i++;
        }
        return taskStateArr;
    }

    public boolean ॱ() {
        amm.ˏ(!this.ˋॱ);
        if (!this.ˏॱ) {
            return false;
        }
        for (int i = 0; i < this.ॱॱ.size(); i++) {
            if (((Task) this.ॱॱ.get(i)).ˊ()) {
                return false;
            }
        }
        return true;
    }

    private Task ˏ(ahp o_ahp) {
        int i = this.ͺ;
        this.ͺ = i + 1;
        Task task = new Task(i, this, o_ahp, this.ॱ);
        this.ॱॱ.add(task);
        ˊ("Task is added", task);
        return task;
    }

    private void ᐝ() {
        if (this.ˏॱ && !this.ˋॱ) {
            Object obj;
            if (this.ॱˊ || this.ʽ.size() == this.ˋ) {
                obj = 1;
            } else {
                obj = null;
            }
            int i = 0;
            Object obj2 = obj;
            while (i < this.ॱॱ.size()) {
                Object obj3;
                Task task = (Task) this.ॱॱ.get(i);
                if (task.ॱॱ()) {
                    ahp ॱ = task.ˋ;
                    boolean z = ॱ.ˏ;
                    if (z || obj2 == null) {
                        int i2 = 0;
                        Object obj4 = 1;
                        while (i2 < i) {
                            Task task2 = (Task) this.ॱॱ.get(i2);
                            if (task2.ˋ.ॱ(ॱ)) {
                                if (!z) {
                                    if (task2.ˋ.ˏ) {
                                        obj4 = null;
                                        obj2 = 1;
                                        break;
                                    }
                                }
                                ॱ(task + " clashes with " + task2);
                                task2.ᐝ();
                                obj3 = null;
                                i2++;
                                obj4 = obj3;
                            }
                            obj3 = obj4;
                            i2++;
                            obj4 = obj3;
                        }
                        if (obj4 != null) {
                            task.ʻ();
                            if (!z) {
                                this.ʽ.add(task);
                                obj3 = this.ʽ.size() == this.ˋ ? 1 : null;
                            }
                        }
                        obj3 = obj2;
                    } else {
                        obj3 = obj2;
                    }
                } else {
                    obj3 = obj2;
                }
                i++;
                obj2 = obj3;
            }
        }
    }

    private void ʼ() {
        if (ॱ()) {
            ॱ("Notify idle state");
            Iterator it = this.ʼ.iterator();
            while (it.hasNext()) {
                ((b) it.next()).ˊ(this);
            }
        }
    }

    private void ॱ(Task task) {
        if (!this.ˋॱ) {
            Object obj = !task.ˊ() ? 1 : null;
            if (obj != null) {
                this.ʽ.remove(task);
            }
            ˊ(task);
            if (task.ˋ()) {
                this.ॱॱ.remove(task);
                ॱॱ();
            }
            if (obj != null) {
                ᐝ();
                ʼ();
            }
        }
    }

    private void ˊ(Task task) {
        ˊ("Task state is changed", task);
        TaskState ˏ = task.ˏ();
        Iterator it = this.ʼ.iterator();
        while (it.hasNext()) {
            ((b) it.next()).ˋ(this, ˏ);
        }
    }

    private void ॱॱ() {
        if (!this.ˋॱ) {
            ahp[] o_ahpArr = new ahp[this.ॱॱ.size()];
            for (int i = 0; i < this.ॱॱ.size(); i++) {
                o_ahpArr[i] = ((Task) this.ॱॱ.get(i)).ˋ;
            }
            this.ʻ.post(new ahq(this, o_ahpArr));
        }
    }

    private /* synthetic */ void ॱ(ahp[] o_ahpArr) {
        try {
            this.ˏ.ˎ(o_ahpArr);
            ॱ("Actions persisted.");
        } catch (Throwable e) {
            amz.ˊ("DownloadManager", "Persisting actions failed.", e);
        }
    }

    private static void ॱ(String str) {
    }

    private static void ˊ(String str, Task task) {
        ॱ(str + ": " + task);
    }
}
